include ../makedef.mk

test_files := $(wildcard ./build/gtest*.o)
test_target := $(patsubst %.o,%.o,${test_files})
INC := ${INC} -I../include/ -I../include_api/ -I../include_arch/ -I../include_plat/
#PERF := /usr/bin/time -v
CFLAGS := ${CFLAGS} -Wno-unused-parameter -DRUNTIME_DBG_LVL=0
.PHONY:clean test all

all:$(target)
	@rm -f *.plist

lib:
	@make clean -C ./src
	@make -C ./src

test_larson:
	LD_PRELOAD=./build_lib/glibtestmalloc.so ./bench/mimalloc-bench/out/bench/larson 5 8 1000 5000 100 4141 ${core_nr}
	LD_PRELOAD=./build_lib/glibwcetmalloc.so ./bench/mimalloc-bench/out/bench/larson 5 8 1000 5000 100 4141 ${core_nr}
	LD_PRELOAD=./build_lib/glibwcetmalloc.so ./bench/mimalloc-bench/out/bench/larson 5 8 1000 5000 100 4141 ${core_nr}

test_wcet:
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfslabmalloc.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfspanmalloc.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfspan_wfqpmalloc.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./git3rd/mimalloc-bench/extern/mi/out/release/libmimalloc.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./git3rd/mimalloc-bench/extern/sn/release/libsnmallocshim.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv

test_wcmf:
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfslabmalloc.so ./build/gtest_wcmf.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfspanmalloc.so ./build/gtest_wcmf.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfspan_wfqpmalloc.so ./build/gtest_wcmf.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./git3rd/mimalloc-bench/extern/mi/out/release/libmimalloc.so ./build/gtest_wcmf.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./git3rd/mimalloc-bench/extern/sn/release/libsnmallocshim.so ./build/gtest_wcmf.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv

test_wcet_other:
	/usr/bin/time -v env LD_PRELOAD=./git3rd/mimalloc-bench/extern/mi/out/release/libmimalloc.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv
	/usr/bin/time -v env LD_PRELOAD=./git3rd/mimalloc-bench/extern/sn/release/libsnmallocshim.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv


test_wcet_wfspan:
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfspanmalloc.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv

test_wcet_wfslab:
	/usr/bin/time -v env LD_PRELOAD=./build_lib/cllibwfslabmalloc.so ./build/gtest_wcet.o 2>./log/wcet_test.tsv
	cat ./log/wcet_test.tsv

clean:
	rm ./build/*.o -f
	rm ./preproc/*.o.c -f
	rm ./assembly/*.s -f
	rm ./assembly/*.ssa -f

run:$(test_target)
	
g%.o:cl%.o
	# @echo "test $@ with mutex " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_mutex.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_mutex.so ./$<
	# @echo "test $@ with pspin " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_pspin.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_pspin.so ./$<
	# @echo "test $@ with spin " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_spin.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_spin.so ./$<
	# @echo "test $@ with ticket " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_ticket.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_ticket.so ./$<
	# @echo "test $@ with clh " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_clh.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_clh.so ./$<
	# @echo "test $@ with mcs " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_mcs.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_mcs.so ./$<
	# @echo "test $@ with ccsync " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_ccsync.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_ccsync.so ./$<
	# @echo "test $@ with dsmsync " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_dsmsync.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_dsmsync.so ./$<
	# @echo "test $@ with hsync " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_hsync.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_hsync.so ./$<
	# @echo "test $@ with flat " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_flat.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_flat.so ./$<
	# # @echo "test $@ with gflatn " 
	# # @${PERF} LD_PRELOAD=./build_lib/glibapfsds_gflatn.so ./$@
	# # @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_gflatn.so ./$<

	# @echo "test $@ with rcl " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_rcl.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_rcl.so ./$<

	# @echo "test $@ with cac_ccsyn " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_cacccsyn.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_cacccsyn.so ./$<
	# @echo "test $@ with cac_dsmsyn " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_cacdsmsyn.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_cacdsmsyn.so ./$<
	# @echo "test $@ with cac_hsyn " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_cachsyn.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_cachsyn.so ./$<
	# @echo "test $@ with cac_flat " 
	# @${PERF} LD_PRELOAD=./build_lib/glibapfsds_cacflat.so ./$@
	# @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_cacflat.so ./$<
	# # @echo "test $@ with cac_gflatn " 
	# # @${PERF} LD_PRELOAD=./build_lib/glibapfsds_cac_gflatn.so ./$@
	# # @${PERF} LD_PRELOAD=./build_lib/cllibapfsds_cac_gflatn.so ./$<

%.o:%.c
	@mkdir -p build
	@mkdir -p preproc
	@mkdir -p assembly
	@echo "----generated pre-processed p$@.cpp----"
	@${GCC} ${INC} ${PREDEFINED} $^ -E -P  > ./preproc/g$@.c
	@#${CLANG} ${INC} ${PREDEFINED} $^ -E -P > ./preproc/cl$@.c
	@#${GCC} ${INC} ${PREDEFINED} $^ -fdump-tree-ssa -fverbose-asm -S -o ./assembly/g$@.S
	
	@echo "----run clang static analyzer----"
	@${CLANG} ${INC} ${PREDEFINED} --analyze  $^
	@echo "----compiling using gcc with analyzer----"
	@${GCC} ${INC} ${PREDEFINED}  $^ ${LIBS} ${COPT_FLAGS} ${CFLAGS} ${GCCFLAGS} -o ./build/g$@
	@echo "----compiling using clang----"
	@${CLANG} ${INC} ${PREDEFINED}  $^ ${LIBS} ${COPT_FLAGS} ${CFLAGS} -o ./build/cl$@
	@#${CLANG} ${INC} ${PREDEFINED}  ./preproc/g$@.c ${LIBS} ${COPT_FLAGS} ${CFLAGS} -o ./build/clg$@
